﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class GlobalSettingDA : IGlobalSettingDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string GLOBAL_SETTING_ID = "@GLOBAL_SETTING_ID";
        private string SQL_CHECK_GLOBAL_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM  SYSADM.GLOBAL_SETTING WHERE GLOBAL_SETTING_ID = @GLOBAL_SETTING_ID ";
        private string SQL_DELETE_GLOBAL_SETTING = " DELETE FROM  SYSADM.GLOBAL_SETTING WHERE GLOBAL_SETTING_ID = @GLOBAL_SETTING_ID ";
        private string SQL_INSERT_GLOBAL_SETTING = " INSERT INTO  SYSADM.GLOBAL_SETTING ( GLOBAL_SETTING_ID, DESCR, VALUE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @GLOBAL_SETTING_ID, @DESCR, @VALUE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_GLOBAL_SETTING = " SELECT  ROWID, GLOBAL_SETTING_ID, DESCR, VALUE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  SYSADM.GLOBAL_SETTING ";
        private string SQL_UPDATE_GLOBAL_SETTING = " UPDATE  SYSADM.GLOBAL_SETTING SET DESCR = @DESCR, VALUE = @VALUE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE GLOBAL_SETTING_ID = @GLOBAL_SETTING_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VALUE = "@VALUE";

        public bool CheckGlobalSettingIDUnique(string globalSettingID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_GLOBAL_SETTING_ID_UNIQUE, paras).ToString() == "1");
        }

        public bool CheckGlobalSettingIDUnique(DataBase dataBase, DbTransaction tran, string globalSettingID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_GLOBAL_SETTING_ID_UNIQUE, paras).ToString() == "1");
        }

        public int DeleteGlobalSetting(List<string> globalSettingID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < globalSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_GLOBAL_SETTING, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteGlobalSetting(DataBase dataBase, DbTransaction tran, List<string> globalSettingID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < globalSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_GLOBAL_SETTING, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllGlobalSetting()
        {
            string sql = this.SQL_SELECT_ALL_GLOBAL_SETTING;
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllGlobalSetting(DataBase dataBase, DbTransaction tran)
        {
            string sql = this.SQL_SELECT_ALL_GLOBAL_SETTING;
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<GlobalSettingInfo> GetALLGlobalSetting()
        {
            string sql = this.SQL_SELECT_ALL_GLOBAL_SETTING;
            List<GlobalSettingInfo> globalSettingList = new List<GlobalSettingInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql))
            {
                while (reader.Read())
                {
                    DateTime CS00001;
                    GlobalSettingInfo globalSettingInfo = new GlobalSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["GLOBAL_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00001 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00001 = new DateTime()));
                    globalSettingList.Add(globalSettingInfo);
                }
            }
            return globalSettingList;
        }

        public GlobalSettingInfo GetGlobalSettingByID(string globalsettingID)
        {
            string sql = this.SQL_SELECT_ALL_GLOBAL_SETTING + " WHERE GLOBAL_SETTING_ID = @GLOBAL_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalsettingID) };
            GlobalSettingInfo globalSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    globalSettingInfo = new GlobalSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["GLOBAL_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return globalSettingInfo;
        }

        public GlobalSettingInfo GetGlobalSettingByID(DataBase dataBase, DbTransaction tran, string globalsettingID)
        {
            string sql = this.SQL_SELECT_ALL_GLOBAL_SETTING + " WHERE GLOBAL_SETTING_ID = @GLOBAL_SETTING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GLOBAL_SETTING_ID", globalsettingID) };
            GlobalSettingInfo globalSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                globalSettingInfo = new GlobalSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["GLOBAL_SETTING_ID"].ToString(), reader["DESCR"].ToString(), reader["VALUE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return globalSettingInfo;
        }

        public DataSet GetGlobalSettingByQueryList(List<string> sqlWhere, GlobalSettingQueryEntity globalsettingQuery)
        {
            string temp = this.SQL_SELECT_ALL_GLOBAL_SETTING;
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!globalsettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, globalsettingQuery.CurrentPage, globalsettingQuery.PageSize, globalsettingQuery.SortField, globalsettingQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetGlobalSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, GlobalSettingQueryEntity globalsettingQuery)
        {
            string temp = this.SQL_SELECT_ALL_GLOBAL_SETTING;
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, globalsettingQuery.CurrentPage, globalsettingQuery.PageSize, globalsettingQuery.SortField, globalsettingQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertGlobalSetting(GlobalSettingInfo globalSettingInfo)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_GlobalSetting_Parameters(globalSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GLOBAL_SETTING, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertGlobalSetting(DataBase dataBase, DbTransaction tran, GlobalSettingInfo globalSettingInfo)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_GlobalSetting_Parameters(globalSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GLOBAL_SETTING, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_GlobalSetting_Parameters(GlobalSettingInfo globalSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.GLOBAL_SETTING_ID, SqlDbType.VarChar, 300), new SqlParameter(this.DESCR, SqlDbType.VarChar, 500), new SqlParameter(this.VALUE, SqlDbType.VarChar, 300), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 50), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 50) };
            paramArray[0].Value = globalSettingInfo.GlobalSettingID;
            if (!string.IsNullOrEmpty(globalSettingInfo.Descr))
            {
                paramArray[1].Value = globalSettingInfo.Descr;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(globalSettingInfo.Value))
            {
                paramArray[2].Value = globalSettingInfo.Value;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(globalSettingInfo.CreatedBy))
            {
                paramArray[3].Value = globalSettingInfo.CreatedBy;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(globalSettingInfo.UpdatedBy))
            {
                paramArray[4].Value = globalSettingInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[4].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateGlobalSetting(GlobalSettingInfo globalSettingInfo)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_GlobalSetting_Parameters(globalSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GLOBAL_SETTING, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateGlobalSetting(DataBase dataBase, DbTransaction tran, GlobalSettingInfo globalSettingInfo)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_GlobalSetting_Parameters(globalSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GLOBAL_SETTING, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

